from django import template
from shop.utils import category_tree_for_node

register = template.Library()

class CategoryTreeForNodeNode(template.Node):
    def __init__(self, current_category_var, context_var):
        self.current_category_var = current_category_var
        self.context_var = context_var

    def render(self, context):
        current_category = context[self.current_category_var]
        context[self.context_var] = category_tree_for_node(current_category)
        return ''

def do_shop_category_tree_for_node(parser, token):
    bits = token.contents.split()
    return CategoryTreeForNodeNode(bits[1], bits[3])

@register.filter
def get_range(value):
    return range(value)

register.tag('shop_category_tree_for_node', do_shop_category_tree_for_node)